import 'package:flutter/material.dart';
import 'package:ref/ref.dart';
import 'state.dart';

class TestWatch extends StatelessWidget {
  const TestWatch({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Text('-----测试watch-----'),
        Wrap(
          children: [
            MvvmDemo01(),
            MvvmDemo02(),
          ],
        )
      ],
    );
  }
}

// -------------------------------------------------------------------------------------------------------
class MvvmDemo01 extends StatefulWidget {
  const MvvmDemo01({Key? key}) : super(key: key);
  @override
  _MvvmDemo01State createState() => _MvvmDemo01State();
}

class _MvvmDemo01State extends State<MvvmDemo01> {
  add() {
    if (wacthCount.value < 11) wacthCount.value++;
  }

  minis() {
    if (wacthCount.value > 0) wacthCount.value--;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width / 3,
      color: Colors.black.withOpacity(0.1),
      padding: EdgeInsets.all(5),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          RefBuilder(
            () => Text(wacthCount.value.toString(),
                style: TextStyle(fontSize: 20)),
          ),
          TextButton(onPressed: add, child: Text('加分')),
          TextButton(onPressed: minis, child: Text('减分')),
        ],
      ),
    );
  }
}

// ---------------------------------------------------------------------------------------------------
class MvvmDemo02 extends StatefulWidget {
  const MvvmDemo02({Key? key}) : super(key: key);
  @override
  _MvvmDemo02State createState() => _MvvmDemo02State();
}

class _MvvmDemo02State extends State<MvvmDemo02> {
  late RefWatch watchFun;
  String score = '比赛开始';

  @override
  void initState() {
    super.initState();
    watchFun = RefWatch(() => wacthCount.value, (oldVal, newVal) {
      String desc = '';
      if (newVal == 0) {
        desc = '大鸭蛋';
      } else if (newVal < 5) {
        desc = '没及格啊';
      } else if (newVal < 8) {
        desc = '还需继续努力';
      } else if (newVal < 10) {
        desc = 'good';
      } else if (newVal < 11) {
        desc = '你比爱因斯坦还牛';
      } else {
        desc = '顶天了';
      }
      score = '上一次$oldVal分,这一次$newVal分。$desc';
      setState(() {});
    });
  }

  @override
  void dispose() {
    // 清除watch依赖
    watchFun.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width / 3 * 2,
      color: Colors.black.withOpacity(0.2),
      padding: EdgeInsets.all(5),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text("满分十分，看你能得几分", style: TextStyle(fontSize: 14)),
          Text(score, style: TextStyle(fontSize: 16, color: Colors.red)),
        ],
      ),
    );
  }
}
